package com.start.security.data.permission;


import com.start.common.details.BaseUserDetails;
import com.start.common.vo.SysMenuDataRulesVo;
import com.start.security.StartUserPermissionTools;
import com.start.security.annotations.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 动态表单数据权限注入器
 */
public class DataStartPermissionComponentAndVariableType implements StartPermissionInjector {
    private StartUserPermissionTools userPermissionTools;

    public DataStartPermissionComponentAndVariableType(StartUserPermissionTools userPermissionTools) {
        this.userPermissionTools = userPermissionTools;
    }

    @Override
    public List<SysMenuDataRulesVo> getRules(HttpServletRequest request, BaseUserDetails currentUser, DataPermission dataPermission) {
        Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        String variableValue = (String) pathVariables.getOrDefault(dataPermission.variable(), null);
        if (StringUtils.isEmpty(dataPermission.value())) return null;
        if (StringUtils.isEmpty(variableValue)) return null;
        return userPermissionTools.getUserMenuDataRulesByDynamicFormId(currentUser,dataPermission.variable());
    }
}
